home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / ini / profile.pas < prev    next >
Pascal/Delphi Source File  |  1995-12-22  |  5KB  |  202 lines

  1. unit Profile;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinProcs, Classes, WinTypes, Messages, Graphics, Controls;
  7.  
  8. type
  9.   TProfile = class(TComponent)
  10.   private
  11.     FDefaultText: string;
  12.     FEntry: string;
  13.     FFileName: string;
  14.     FSection: string;
  15.     FState: boolean;
  16.     FText: string;
  17.     FValue: Longint;
  18.     procedure SetDefaultText(strText: string);
  19.     procedure SetEntry(strEntry: string);
  20.     procedure SetFileName(strFileName: string);
  21.     procedure SetSection(strSection: string);
  22.     function GetState: boolean;
  23.     procedure SetState(bState: boolean);
  24.     function GetText: string;
  25.     procedure SetText(strText: string);
  26.     function GetValue: Longint;
  27.     procedure SetValue(lValue: Longint);
  28.   public
  29.     constructor Create(AOwner: TComponent); override;
  30.   published
  31.     property DefaultText: string read FDefaultText write SetDefaultText;
  32.     property Entry: string read FEntry write SetEntry;
  33.     property FileName: string read FFileName write SetFileName;
  34.     property Section: string read FSection write SetSection;
  35.     property State: boolean read GetState write SetState;
  36.     property Text: string read GetText write SetText;
  37.     property Value: longint read GetValue write SetValue;
  38.   end;
  39.  
  40. procedure Register;
  41.  
  42. implementation
  43.  
  44. procedure Register;
  45. begin
  46.   RegisterComponents('Additional', [TProfile]);
  47. end;
  48.  
  49. function StrToBool(strVal: string): boolean;
  50. begin
  51.   if (UpperCase(strVal) = 'TRUE') or (UpperCase(strVal) = 'YES') or (strVal = '1') then
  52.     Result := True
  53.   else
  54.     Result := False;
  55. end;
  56.  
  57. function BoolToStr(bVal: boolean): string;
  58. begin
  59.   if bVal then
  60.     Result := 'True'
  61.   else
  62.     Result := 'False';
  63. end;
  64.  
  65. constructor TProfile.Create(AOwner: TComponent);
  66. begin
  67.   inherited Create(AOwner);
  68.   FDefaultText := '';
  69.   FEntry := '';
  70.   FFileName := 'WIN.INI';
  71.   FSection := '';
  72.   FState := False;
  73.   FText := '';
  74.   FValue := 0;
  75. end;
  76.  
  77. procedure TProfile.SetDefaultText(strText: string);
  78. begin
  79.   if strText <> FDefaultText then
  80.     FDefaultText := strText;
  81. end;
  82.  
  83. procedure TProfile.SetEntry(strEntry: string);
  84. begin
  85.   if strEntry <> FEntry then
  86.     FEntry := strEntry;
  87. end;
  88.  
  89. procedure TProfile.SetFileName(strFileName: string);
  90. begin
  91.   if strFileName <> FFileName then
  92.     FFileName := strFileName;
  93. end;
  94.  
  95. procedure TProfile.SetSection(strSection: string);
  96. begin
  97.   if strSection <> FSection then
  98.     FSection := strSection;
  99. end;
  100.  
  101. function TProfile.GetState: boolean;
  102. begin
  103.   Result := StrToBool(GetText);
  104. end;
  105.  
  106. procedure TProfile.SetState(bState: boolean);
  107. begin
  108.   SetText(BoolToStr(bState));
  109. end;
  110.  
  111. function TProfile.GetText: string;
  112. var
  113.   szSection, szEntry, szDefaultText, szText, szFile: PChar;
  114. begin
  115.   if (FSection <> '') and (FEntry <> '') and (FFileName <> '') then
  116.   begin
  117.     { Allocate some memory for PChars }
  118.     szSection := StrAlloc(256);
  119.     szEntry := StrAlloc(256);
  120.     szDefaultText := StrAlloc(256);
  121.     szText := StrAlloc(256);
  122.     szFile := StrAlloc(256);
  123.  
  124.     try
  125.       { Copy our properties into the PChars }
  126.       StrPCopy(szSection, FSection);
  127.       StrPCopy(szEntry, FEntry);
  128.       StrPCopy(szDefaultText, FDefaultText);
  129.       StrPCopy(szFile, FFileName);
  130.  
  131.       { Read the profile string }
  132.       GetPrivateProfileString(szSection, szEntry, szDefaultText, szText, 256, szFile);
  133.       FText := StrPas(szText);
  134.       Result := FText;
  135.     finally
  136.       StrDispose(szSection);
  137.       StrDispose(szEntry);
  138.       StrDispose(szText);
  139.       StrDispose(szDefaultText);
  140.       StrDispose(szFile);
  141.     end;
  142.   end
  143.   else
  144.   begin
  145.     FText := '';
  146.     Result := '';
  147.   end;
  148. end;
  149.  
  150. procedure TProfile.SetText(strText: string);
  151. var
  152.   szEntry, szFile, szSection, szText: PChar;
  153.   Success: Bool;
  154. begin
  155.   if (FSection <> '') and (FEntry <> '') and (FFileName <> '') then
  156.   begin
  157.     { Allocate some memory for PChars }
  158.     szSection := StrAlloc(256);
  159.     szEntry := StrAlloc(256);
  160.     szText := StrAlloc(256);
  161.     szFile := StrAlloc(256);
  162.  
  163.     try
  164.       { Copy our properties into the PChars }
  165.       StrPCopy(szSection, FSection);
  166.       StrPCopy(szEntry, FEntry);
  167.       StrPCopy(szText, strText);
  168.       StrPCopy(szFile, FFileName);
  169.  
  170.       { Write the profile string }
  171.       Success := WritePrivateProfileString(szSection, szEntry, szText, szFile);
  172.  
  173.       if Success then
  174.         FText := strText;
  175.     finally
  176.       StrDispose(szEntry);
  177.       StrDispose(szFile);
  178.       StrDispose(szSection);
  179.       StrDispose(szText);
  180.     end;
  181.   end;
  182. end;
  183.  
  184. function TProfile.GetValue: longint;
  185. begin
  186.   try
  187.     Result := StrToInt(GetText);
  188.   except
  189.     if StrToBool(GetText) then
  190.       Result := 1
  191.     else
  192.       Result := 0;
  193.   end;
  194. end;
  195.  
  196. procedure TProfile.SetValue(lValue: longint);
  197. begin
  198.   SetText(IntToStr(lValue));
  199. end;
  200.  
  201. end.
  202.